<template>
    <el-autocomplete style="width:100%" v-model="code" :debounce="0" :fetch-suggestions="querySearch" :placeholder="placeholder" :clearable="clearable" :disabled="disabled" @select="handleSelect"></el-autocomplete>
</template>

<script>
    export default {
        data() {
            return {
                code: this.value,
                accountList: []
            };
        },
        props: {
            type: {
                type: Object,
                default: ()=> {}
            },
            value: {
                type: [String, Array],
                default: ''
            },
            placeholder: {
                type: String,
                default: ''
            },
            disabled: {
                type: Boolean,
                default: false
            },
            clearable: {
                type: Boolean,
                default: true
            },
            multiple: {
                type: Boolean,
                default: false
            },
            filterable: {
                type: Boolean,
                default: false
            }
        },
        watch: {
            code(val, oldVal) {
                if (val !== oldVal) {
                    this.$emit("input", this.code);
                    this.$emit("change");
                }
            },
            value(val, oldVal) {
                this.code = val;
            },
            type(val) {
                if (val.systemType === 'collection') {
                    this.getCollectionAccountList(val);
                }else if(val.systemType === 'audit') {
                    this.getAuditAccountList(val);
                }else if (val.systemType === 'recruiter') {
                    this.getRecruiterList(val)
                }
            }
        },
        methods: {
            // 获取催收系统各角色下账号列表
            getCollectionAccountList(data) {
                const params = {
                    isPage: 1,
                    page: {
                        pageNo: 1,
                        pageSize: 10000
                    },
                    roleCode: data.roleCode, // 角色类型
                    collectionUserName: '', // 账号名称
                    active: ''  //账号状态
                };
                this.$api.authorityManage.queryCollectorsNew(params).then((res) => {
                    const { status, data, error } = res;
                    if ('1' === status && '00000000' === error && data) {
                        data.resultData.forEach(item => {
                            item.value = item.collectionUserName;
                        });
                        this.accountList = data.resultData;
                    }
                });
            },
            // 获取催收员、预催收员账号列表
            getRecruiterList(data) {
                const params = {
                    roleCode: data.roleCode, // 角色类型
                };
                this.$api.collection.queryCollectorList(params).then((res) => {
                    const { status, data, error } = res;
                    if ('1' === status && '00000000' === error && data) {
                        data.forEach(item => {
                            item.value = item.collectionUserName;
                        });
                        this.accountList = data;
                    }
                });
            },
            // 获取信审系统各角色下账号列表
            getAuditAccountList(data) {
                const params = {
                    type: data.roleType,
                    name: '',
                    isDelete: '',
                    leaderId: data.leaderId,
                    page: {
                        pageNo: 1,
                        pageSize: 10000
                    },
                };
                this.$api.authorityManage.getAllAuditorInfo(params).then((res) => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error && data.data) {
                        data.data.forEach(item => {
                            item.value = item.name;
                        });
                        this.accountList = data.data;
                    }
                });
            },
            querySearch(queryString, cb) {
                let accountList = this.accountList;
                let results = queryString ? accountList.filter(this.createFilter(queryString)) : accountList;
                // 调用 callback 返回建议列表的数据
                cb(results);
            },
            createFilter(queryString) {
                return (account) => {
                    return (account.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                };
            },
            handleSelect(item) {
                console.log(item);
            }
        },
        created() {
            if (this.type.systemType) {
                if (this.type.systemType === 'collection') {
                    this.getCollectionAccountList(this.type);
                }else if(this.type.systemType === 'audit') {
                    this.getAuditAccountList(this.type);
                }else if (this.type.systemType === 'recruiter') {
                    this.getRecruiterList(this.type)
                }
            }
        },
    };
</script>

<style scoped>

</style>